import {getDetails} from '@/api/admin/user'
import { validateStr } from '@/util/validate'

const validateLoginname = (rule, value, callback) => {
  if (validateStr(value)[0]) {
    callback(new Error(validateStr(value)[1]))
  } else {
    getDetails(value).then(response => {
      if (window.boxType === 'edit') callback()
      const result = response.data.data
      if (result !== null) {
        callback(new Error('用户名已经存在'))
      } else {
        callback()
      }
    })
  }

}

const validatePsd = (rule, value, callback) => {
  if (!value){
    callback()
    return
  }
  if (validateStr(value)[0]) {
    callback(new Error(validateStr(value)[1]))
  } else {
    callback()
  }

}

const validateMobilePhone = (rule, value, callback) => {
	   if (value === '') {
	      callback(new Error('手机号不可为空'));
	   } else {
	    if (value !== '') {
	        var reg=/^1[3456789]\d{9}$/;
	        if(!reg.test(value)){
	          callback(new Error('请输入有效的手机号码'));
	        }
	     }
	      callback();
	   }
}

export const tableOption = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  searchMenuSpan:6,
  editBtn: false,
  delBtn: false,
  align: 'center',
  addBtn: false,
  column: [{
    fixed: true,
    label: 'id',
    prop: 'userId',
    span: 24,
    hide: true,
    editDisplay: false,
    addDisplay: false
  },{
    fixed: true,
    label: '登录名',
    prop: 'loginname',
    editDisabled: true,
    slot: true,
    search: true,
    span: 24,
    rules: [{
      required: true,
      message: '请输入登录名'
    },
      {
        min: 2,
        max: 20,
        message: '长度在 2 到 20 个字符',
        trigger: 'blur'
      },
      {validator: validateLoginname, trigger: 'blur'}
    ]
  }, {
    fixed: true,
    label: '用户名',
    prop: 'username',
    editDisabled: true,
    // slot: true,
    search: true,
    maxlength: 20,
    showWordLimit: true,
    span: 24,
    rules: [{
      required: true,
      message: '请输入用户名'
    },
      {
        min: 2,
        max: 20,
        message: '长度在 2 到 20 个字符',
        trigger: 'blur'
      }
    ]
  }, {
    label: '密码',
    prop: 'password',
    type: 'password',
    value: '',
    hide: true,
    span: 24,
    // rules: [{
    //   min: 6,
    //   max: 20,
    //   message: '长度在 6 到 20 个字符',
    //   trigger: 'blur'
    // },
      //{validator: validatePsd, trigger: 'blur'},
	  // ]
  }, {
    label: '所属部门',
    prop: 'deptId',
    formslot: true,
    slot: true,
    span: 24,
    hide: true,
    dataType:"number",
    rules: [{
      required: true,
      message: '请选择部门',
      trigger: 'change'
    }]
  }, {
    label: '手机号',
    prop: 'phone',
    type: 'tel',
    value: '',
    width: 100,
    span: 24,
    rules: [{
      required: true,
      min: 11,
      max: 11,
      message: '长度在 11 个字符',
      trigger: 'blur'
    },{validator: validateMobilePhone, trigger: 'blur'}]
  }, {
	    label: '邮箱',
	    prop: 'email',
	    hide: true,
	    span: 24,
	    rules: [{
	      min: 2,
	      max: 50,
	      message: '长度在2到 50 个字符',
	      trigger: 'blur'
	    }]
   },{
    label: '角色',
    prop: 'role',
    formslot: true,
    slot: true,
    overHidden: true,
    span: 24,
    rules: [{
      required: true,
      message: '请选择角色',
      trigger: 'change'
    }]
  }, {
    label: '状态',
    prop: 'lockFlag',
    type: 'radio',
    slot: true,
    border:true,
    span: 24,
    rules: [{
      required: true,
      message: '请选择状态',
      trigger: ['change','blur']
    }],
    value: '0',
    dicData: [{
      label: '有效',
      value: '0'
    }, {
      label: '锁定',
      value: '9'
    }]
  }, {
    width: 180,
    label: '创建时间',
    prop: 'createTime',
    type: 'datetime',
    format: 'yyyy-MM-dd HH:mm',
    valueFormat: 'yyyy-MM-dd HH:mm:ss',
    editDisabled: true,
    addDisplay: false,
    span: 24
  }]
}
